package org.jground.rx.ui.zk.viewControl;

import org.jground.rx.RX;
import org.jground.rx.RXType;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttributeView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.util.ArrayUtils;
import org.jground.util.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Label;

public class UIAttributeViewString extends UIAttributeViewBase {

    private boolean isPercentage;
    private Label label;
    private boolean isPassword;
    private Integer maxcols;

    private UIAttributeViewString(UIFrame uiFrame, RXView moView) {
        super(uiFrame, moView);
        isPercentage = RXType.PERCENTAGE.equals(moView.getRXAttribute().getRXType().getBaseType().getTypeId());
        isPassword = RX.AttValue.password.equals(moView.getProperty(RX.AttName.display));
        add(label = new Label());
        label.setStyle("white-space:normal;");
        try {
            maxcols = new Integer(moView.getProperty(RX.AttName.maxcols));
        } catch (NumberFormatException e) {}
    }

    public UIAttributeViewString() {}

    @Override
    public void set(String... values) {
        clear();
        if (ArrayUtils.isNotEmpty(values)) {
            String v = values[0];
            if (isPassword) {
                v = StringUtils.toStars(v);
            } else {
                v = v + (isPercentage ? "%" : "");
            }
            if (maxcols != null) {
                v = StringUtils.cutToMax(v, maxcols);
            }
            label.setValue(v);
            label.setMultiline(true);
            renderToStringS.add(v);
        }
    }

    @Override
    public void clear() {
        label.setValue("");
        renderToStringS.clear();
    }

    public IUIAttributeView<Component, UIFrame> createNew(UIFrame uiFrame, RXView moControl) {
        return new UIAttributeViewString(uiFrame, moControl);
    }
}